package com.leilei.serviceProject.domain.strategy.model.entity;

import com.leilei.serviceProject.types.common.Constants;
import com.leilei.serviceProject.types.exception.AppException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yanglei
 * @description
 * @create 2024/10/21 16:20
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class StrategyRuleEntity {
    /**
     * 抽奖策略ID
     */
    private Long strategyId;
    /**
     * 抽奖奖品ID【规则类型为策略，则不需要奖品ID】
     */
    private Integer awardId;
    /**
     * 抽象规则类型；1-策略规则、2-奖品规则
     */
    private Integer ruleType;
    /**
     * 抽奖规则类型【rule_random - 随机值计算、rule_lock - 抽奖几次后解锁、rule_luck_award - 幸运奖(兜底奖品)】
     */
    private String rulemodel;
    /**
     * 抽奖规则比值
     */
    private String ruleValue;
    /**
     * 抽奖规则描述
     */
    private String ruleDesc;

    public Map<String, List<Integer>> getRuleValue(String ruleValue) {
        String[] ruleValues = ruleValue.split(Constants.SPACE);
        Map<String, List<Integer>> ruleValueMap = new HashMap<>();
        for (String ruleValueGroup : ruleValues) {
            String[] parts = ruleValueGroup.split(Constants.COLON);
            if (2 != parts.length) {
                throw new AppException("rule_weight,rule_value invalid input format", ruleValueGroup);
            }
            String[] valueStrings = parts[1].split(Constants.SPLIT);
            List<Integer> valueList = new ArrayList<>();
            for (String valueString : valueStrings) {
                valueList.add(Integer.valueOf(valueString));
            }
            ruleValueMap.put(ruleValueGroup,valueList);
        }

        return ruleValueMap;
    }
}
